import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicColumn, BasicTable, useTable } from "@/components/Table";
import "./index.less";
import { useEffect, useState } from "react";
import { listTreatmentsDept } from "@/api/SecondmentDictionary";
import { useTranslation } from "react-i18next";
import { drugBatchApi } from "@/api/MedicalExamination/recipe";
import { message } from "antd";
const TreatmentPhmList = (props: { register: Fn; success?: Fn }) => {
	const { t } = useTranslation();
	const columns: BasicColumn[] = [
		{
			title: t("MedicalExamination.recipe.treatmentPlan"),
			dataIndex: "trtmtName"
		},
		{
			title: t("MedicalExamination.recipe.treatmentContent"),
			dataIndex: "trtmtContent"
		},
		{
			title: t("MedicalExamination.recipe.patient"),
			dataIndex: "patientId"
		},
		{
			title: t("MedicalExamination.recipe.doctor"),
			dataIndex: "doctorId"
		}
	];
	const [trtmtId, setTrtmtId] = useState("");
	const [dataSource, setDataSource] = useState([]);
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		setTrtmtId(data.trtmtId);
	});

	const handleSubmit = async () => {
		const res = await drugBatchApi.drugBatchDispensing({ trtmtId });
		if (!res) message.error(t("kevin_ui.addFailed"));
		message.success(t("kevin_ui.addedSuccessfully"));
		closeDrawer();
	};
	const [register] = useTable({
		dataSource,
		columns,
		useSearchForm: false,
		bordered: false
	});
	const getTreatmentsDept = async () => {
		const res = await listTreatmentsDept({ trtmtId });
		console.log(res);
		setDataSource(res.list);
	};
	useEffect(() => {
		if (!trtmtId) return;
		console.log(trtmtId);
		getTreatmentsDept();
	}, [trtmtId]);
	return (
		<div>
			<BasicDrawer showFooter={true} size="large" {...props} register={registerDrawer} ok={handleSubmit}>
				<div className="recipe">
					<BasicTable register={register}></BasicTable>
				</div>
			</BasicDrawer>
		</div>
	);
};

export default TreatmentPhmList;
